﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Orchard;
using Orchard.ContentManagement;
using Orchard.ContentManagement.MetaData;
using Orchard.Data;
using Orchard.Settings;
using Orchard.DisplayManagement;
using MediaGarden.Services;
using Downplay.Origami;
using MediaGarden.Models;
using MediaGarden.Playlists.Services;
using Orchard.Localization;
using Orchard.Logging;
using Orchard.Mvc;
using Downplay.Origami.Services;

namespace MediaGarden.Playlists.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    public class PlaylistController : Controller
    {
        public IOrchardServices Services { get; set; }

        public Localizer T { get; set; }
        public ILogger Logger { get; set; }

        private readonly IContentManager _contentManager;
        private readonly IContentDefinitionManager _contentDefinitionManager;
        private readonly ITransactionManager _transactionManager;
        private readonly ISiteService _siteService;
        private readonly IMediaGardenService _gardenService;
        private readonly IOrigamiService _origamiService;
        private readonly IPlaylistService _playlistService;

        dynamic Shape { get; set; }

        public PlaylistController(
                IOrchardServices services,
                IContentManager contentManager,
                IContentDefinitionManager contentDefinitionManager,
                ITransactionManager transactionManager,
                ISiteService siteService,
                IShapeFactory shapeFactory,
                IMediaGardenService gardenService,
                IOrigamiService origamiService,
                IPlaylistService playlistService
        ){
            Services = services;
            _contentManager = contentManager;
            _contentDefinitionManager = contentDefinitionManager;
            _transactionManager = transactionManager;
            _siteService = siteService;
            _gardenService = gardenService;
            _origamiService = origamiService;
            _playlistService = playlistService;
            Shape = shapeFactory;
            T = NullLocalizer.Instance;
            Logger = NullLogger.Instance;
        }

        /// <summary>
        /// Main entry point for loading playlists from content items
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Index(int id, string playlistFormat = null)
        {

            var item = _contentManager.Get(id);
            if (item == null) return HttpNotFound("Playlist does not exist");

            var part = item.As<MediaPart>();
            if (part == null) return HttpNotFound("Playlist does not exist");

            return Playlist(part,playlistFormat);
        }

        [HttpGet]
        public ActionResult Preview(int id, string playlistFormat = null)
        {
            // TODO: Check ownership of source item instead
            if (!Services.Authorizer.Authorize(MediaGarden.Permissions.ImportMedia, T("Couldn't preview playlist")))
                return new HttpUnauthorizedResult();

            var source = _gardenService.GetSource(id);
            if (source == null) return HttpNotFound("Playlist does not exist");
            var preview = _gardenService.MediaPreviewItemFromSource(source);
            return Playlist(preview, playlistFormat);
        }

        /// <summary>
        /// Item as playlist handler
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Item(int id, string playlistFormat = null)
        {
            var item = _contentManager.Get(id);
            if (item == null) return HttpNotFound("Item does not exist");

            var part = item.As<MediaPart>();
            if (part == null) return HttpNotFound("Item does not exist");

            return ItemPlaylist(part, playlistFormat);
        }
        /// <summary>
        /// Item as preview playlist handler
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult PreviewItem(int id, string playlistFormat = null)
        {
            // TODO: Check ownership of source item instead
            if (!Services.Authorizer.Authorize(MediaGarden.Permissions.ImportMedia, T("Couldn't preview item")))
                return new HttpUnauthorizedResult();

            var source = _gardenService.GetSource(id);
            if (source == null) return HttpNotFound("Item does not exist");
            var preview = _gardenService.MediaPreviewItemFromSource(source);
            return ItemPlaylist(preview, playlistFormat);
        }

        protected ActionResult Playlist(IMediaItem item,string playlistFormat)
        {
            var routeValues = ControllerContext.RouteData.Values;

            return Playlist(_playlistService.BuildPlaylist(item, routeValues));
        }
        protected ActionResult ItemPlaylist(IMediaItem item,string playlistFormat)
        {
            var routeValues = ControllerContext.RouteData.Values;

            var playlist = _playlistService.SingleItemPlaylist(item, routeValues);
            playlist.PlaylistFormat = playlistFormat;
            return Playlist(playlist);
        }

        protected ActionResult Playlist(PlaylistContext context)
        {
            return _playlistService.MvcWritePlaylist(this,context);
        }

    }
}